﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SimpleJob.cs" company="DHGMS Solutions">
//   Copyright 2004-2014 DHGMS Solutions.
//      
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//      
//   http://www.apache.org/licenses/LICENSE-2.0
//      
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Jobs
{
    using System;
    using System.Configuration;
    using Dhgms.TplHelper.Helpers;
    using Dhgms.TplHelper.Model.Info;
    using Dhgms.TplHelper.Model.Info.JobResult;
    using Dhgms.TplHelper.Model.TaskHelper;

    /// <summary>
    /// Simple Job is for a Job that runs a single task
    /// </summary>
    public abstract class SimpleJob<TJobResultClass, TJobSettings, TTaskClass, TTaskResultClass> : JobBase<TJobResultClass, TJobSettings>
        where TJobResultClass : SingleTask<TTaskResultClass>, new()
        where TJobSettings : ConfigurationSection
        where TTaskClass : BaseTaskHelper<TTaskResultClass>
        where TTaskResultClass : TaskResultBase, new()
    {
        /// <summary>Initializes a new instance of the <see cref="SimpleJob{TJobResultClass,TJobSettings,TTaskClass,TTaskResultClass}"/> class.</summary>
        /// <param name="guid">The unique id for the job.</param>
        /// <param name="friendlyName">The friendly name.</param>
        protected SimpleJob(Guid guid, string friendlyName)
            : base(guid, friendlyName)
        {
        }

        /// <summary>
        /// The on execute event, where the donkey work should be done
        /// </summary>
        /// <param name="applicationSettings">The application settings</param>
        /// <returns>Job Result Data</returns>
        protected override TJobResultClass OnExecute(TJobSettings applicationSettings)
        {
            var jobResult = new TJobResultClass();
            var task = this.GetTask();
            jobResult.TaskResult = task.GetTaskResult().Result;
            return jobResult;
        }

        /// <summary>
        /// Gets the task instance to be run
        /// </summary>
        /// <returns>The task</returns>
        protected abstract TTaskClass GetTask();
    }
}
